package CCRMClient.GUI;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Image;

import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.SystemColor;

import javax.swing.JTextArea;
import javax.swing.JFileChooser;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.CatalogManager;
import Common.SERVER_COMMANDS;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * This Class implements the Add Product UC
 * @author Ori Ziv
 *
 */
public class AddProductScreen extends JPanel {
	private JLabel lblAddNewProduct;
	private JLabel lblProductName;
	private JLabel lblProductDescription;
	private JLabel lblProductBasePrice;
	private JLabel lblProductPicture;
	private JButton btnSubmitNewProduct;
	private JButton btnCancelAction;
	private JTextField textProductName;
	private JTextArea textAreaProductDescription;
	private JTextField textBasePrice;
	private JButton btnBrowse;
	private JFileChooser fchProductPictureChooser;
	private File picture;
	private JLabel lblPicture;
	private Image productPicture;
	private JLabel lblMandatoryFields;
	private JLabel lblPicturesTitle;
	private JTextField textPicturesTitle;

	/**
	 * Create the panel.
	 */
	public AddProductScreen() {

		initialize();
	}
	private void initialize() {
		setSize (606,700);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblAddNewProduct());
		add(getLblMandatoryFields());
		add(getLblProductName());
		add(getLblProductDescription());
		add(getLblProductBasePrice());
		add(getLblProductPicture());
		add(getBtnSubmitNewProduct());
		add(getBtnCancelAction());
		add(getTextProductName());
		add(getTextAreaProductDescription());
		add(getTextBasePrice());
		add(getBtnBrowse());
		add(getLblPicturesTitle());
		add(getTextPicturesTitle());
		add(getLblPicture());
	}
	JLabel getLblAddNewProduct() {
		if (lblAddNewProduct == null) {
			lblAddNewProduct = new JLabel("Add New Product");
			lblAddNewProduct.setHorizontalAlignment(SwingConstants.CENTER);
			lblAddNewProduct.setForeground(Color.RED);
			lblAddNewProduct.setFont(new Font("David", Font.BOLD, 26));
			lblAddNewProduct.setBackground(Color.WHITE);
			lblAddNewProduct.setBounds(0, 0, 565, 49);
		}
		return lblAddNewProduct;
	}
	private JLabel getLblMandatoryFields() {
		if (lblMandatoryFields == null) {
			lblMandatoryFields = new JLabel("All fields are mandatory!");
			lblMandatoryFields.setForeground(Color.RED);
			lblMandatoryFields.setFont(new Font("David", Font.BOLD, 12));
			lblMandatoryFields.setBounds(10, 46, 175, 14);
		}
		return lblMandatoryFields;
	}
	JLabel getLblProductName() {
		if (lblProductName == null) {
			lblProductName = new JLabel("Product Name:");
			lblProductName.setFont(new Font("David", Font.BOLD, 20));
			lblProductName.setBounds(10, 60, 164, 29);
		}
		return lblProductName;
	}
	JLabel getLblProductDescription() {
		if (lblProductDescription == null) {
			lblProductDescription = new JLabel("Product Description:");
			lblProductDescription.setFont(new Font("David", Font.BOLD, 20));
			lblProductDescription.setBounds(10, 100, 205, 29);
		}
		return lblProductDescription;
	}
	JLabel getLblProductBasePrice() {
		if (lblProductBasePrice == null) {
			lblProductBasePrice = new JLabel("Product's Base Price:");
			lblProductBasePrice.setFont(new Font("David", Font.BOLD, 20));
			lblProductBasePrice.setBounds(10, 248, 205, 29);
		}
		return lblProductBasePrice;
	}
	JLabel getLblProductPicture() {
		if (lblProductPicture == null) {
			lblProductPicture = new JLabel("Product's Picture:");
			lblProductPicture.setFont(new Font("David", Font.BOLD, 20));
			lblProductPicture.setBounds(10, 277, 164, 29);
		}
		return lblProductPicture;
	}
	JButton getBtnSubmitNewProduct() {
		if (btnSubmitNewProduct == null) {
			btnSubmitNewProduct = new JButton("Submit New Product");
			btnSubmitNewProduct.addActionListener(new BtnSubmitNewProductActionListener());
			btnSubmitNewProduct.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSubmitNewProduct.setBounds(10, 550, 195, 23);
		}
		return btnSubmitNewProduct;
	}
	JButton getBtnCancelAction() {
		if (btnCancelAction == null) {
			btnCancelAction = new JButton("Cancel Action");
			btnCancelAction.addActionListener(new BtnCancelActionActionListener());
			btnCancelAction.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnCancelAction.setBounds(439, 550, 124, 23);
		}
		return btnCancelAction;
	}
	JTextField getTextProductName() {
		if (textProductName == null) {
			textProductName = new JTextField();
			textProductName.setColumns(10);
			textProductName.setBackground(SystemColor.menu);
			textProductName.setBounds(237, 65, 251, 20);
		}
		return textProductName;
	}
	JTextArea getTextAreaProductDescription() {
		if (textAreaProductDescription == null) {
			textAreaProductDescription = new JTextArea();
			textAreaProductDescription.setLineWrap(true);
			textAreaProductDescription.setFont(new Font("Courier New", Font.PLAIN, 11));
			textAreaProductDescription.setBorder(new LineBorder(new Color(0, 0, 0)));
			textAreaProductDescription.setBackground(SystemColor.menu);
			textAreaProductDescription.setBounds(76, 128, 412, 109);
		}
		return textAreaProductDescription;
	}
	JTextField getTextBasePrice() {
		if (textBasePrice == null) {
			textBasePrice = new JTextField();
			textBasePrice.setColumns(10);
			textBasePrice.setBackground(SystemColor.menu);
			textBasePrice.setBounds(237, 253, 251, 20);
		}
		return textBasePrice;
	}
	JButton getBtnBrowse() {
		if (btnBrowse == null) {
			btnBrowse = new JButton("Browse");
			btnBrowse.addActionListener(new BtnBrowseActionListener());
			btnBrowse.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnBrowse.setBounds(237, 280, 124, 23);
		}
		return btnBrowse;
	}
	JFileChooser getFchProductPictureChooser() {
		if (fchProductPictureChooser == null) {
			fchProductPictureChooser = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Image .JPG files", "jpg");
			fchProductPictureChooser.setFileFilter(filter);
		}
		return fchProductPictureChooser;
	}
	private JLabel getLblPicturesTitle() {
		if (lblPicturesTitle == null) {
			lblPicturesTitle = new JLabel("Picture's Title:");
			lblPicturesTitle.setFont(new Font("David", Font.BOLD, 20));
			lblPicturesTitle.setBounds(10, 324, 164, 29);
		}
		return lblPicturesTitle;
	}
	private JTextField getTextPicturesTitle() {
		if (textPicturesTitle == null) {
			textPicturesTitle = new JTextField();
			textPicturesTitle.setColumns(10);
			textPicturesTitle.setBackground(SystemColor.menu);
			textPicturesTitle.setBounds(237, 329, 251, 20);
		}
		return textPicturesTitle;
	}
	private JLabel getLblPicture() {
		if (lblPicture == null) {
			lblPicture = new JLabel("");
			lblPicture.setBounds(50, 364, 300, 300);
			setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		}
		return lblPicture;
	}
	/**
	 * This inner class implements event listener for pressing Browse button
	 * @author Ori Ziv
	 *
	 */
	private class BtnBrowseActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int returnVal = getFchProductPictureChooser().showOpenDialog(AddProductScreen.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				picture = fchProductPictureChooser.getSelectedFile();
				BufferedImage bufPic = null;
				try {
					bufPic = ImageIO.read(picture);
					Image fitPic = bufPic.getScaledInstance(lblPicture.getWidth(), lblPicture.getHeight(), Image.SCALE_SMOOTH);
					productPicture = new ImageIcon (fitPic).getImage();
					lblPicture.setIcon(new ImageIcon(productPicture));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Problem loading product picture", "Add Product Picture", JOptionPane.ERROR_MESSAGE);
				}
				
				
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Submit New Product button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSubmitNewProductActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				String productName = textProductName.getText();
				String productDescription = textAreaProductDescription.getText();
				try {
					float productBasePrice = Float.parseFloat(textBasePrice.getText());
					String picturePath = picture.getAbsolutePath();
					String pictureTitle = textPicturesTitle.getText();
					if (productName.isEmpty() || productDescription.isEmpty() || picturePath.isEmpty() || pictureTitle.isEmpty() || picture == null) throw new NullPointerException();
					try {
						ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.ADD_PRODUCT, CatalogManager.addProduct(productName, productDescription, productBasePrice, picturePath, pictureTitle));
					} catch (IOException e) {
						JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
						ClientStart.quitApp();
					}
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "Base price format example: 19.95", "Add product problem", JOptionPane.INFORMATION_MESSAGE);
					textBasePrice.setText("");
				}
			} catch (NullPointerException e) {
				JOptionPane.showMessageDialog(null, "All fields are mandatory", "Add product problem", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Cancel Action button
	 * @author Ori Ziv
	 *
	 */
	private class BtnCancelActionActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CatalogScreen());
		}
	}
}
